package com.rrd.itext.xmlBean.layoutBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.rrd.itext.utils.PDFBuilder;
import com.rrd.itext.utils.StyleUtil;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementParser;
import com.rrd.itext.xmlBean.layoutBean.iface.PrintOrder;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("List")
public class XmlList extends XmlFontStyle implements PrintOrder, PdfElementParser<com.itextpdf.text.List>{
	@XStreamAsAttribute
	private float indent;
	@XStreamAsAttribute
	private float symbolIndent;
	@XStreamAsAttribute
	private int printOrder;
	@XStreamAsAttribute
	private String newLine;
	@XStreamAsAttribute
	private Float leading;
    @XStreamAsAttribute
	private float listItemIndent;
	@XStreamAlias("ListSymbol")
	private XmlListSymbol listSymbol;
	@XStreamAlias("ListItems")
	private List<XmlListItem> listItems;
	
	private Comparator<XmlListItem> listItemComparator;
	
	public Comparator<XmlListItem> getListItemComparator() {
		if(listItemComparator == null){
			this.listItemComparator = new Comparator<XmlListItem>(){
				@Override
				public int compare(XmlListItem o1, XmlListItem o2) {
					return o1.getPrintOrder()-o2.getPrintOrder();
				}};
		}
		return listItemComparator;
	}

	public void setListItemComparator(Comparator<XmlListItem> listItemComparator) {
		this.listItemComparator = listItemComparator;
	}

	public float getIndent() {
		return indent;
	}

	public void setIndent(float indent) {
		this.indent = indent;
	}

	public float getSymbolIndent() {
		return symbolIndent;
	}

	public void setSymbolIndent(float symbolIndent) {
		this.symbolIndent = symbolIndent;
	}

	public XmlListSymbol getListSymbol() {
		return listSymbol;
	}

	public void setListSymbol(XmlListSymbol listSymbol) {
		this.listSymbol = listSymbol;
	}

	
	public int getPrintOrder() {
		return printOrder;
	}


	public void setPrintOrder(int printOrder) {
		this.printOrder = printOrder;
	}


	public boolean getNewLine() {
		if(newLine == null || (!newLine.equalsIgnoreCase("true")))
			return false;
		return true;
	}


	public void setNewLine(String newLine) {
		this.newLine = newLine;
	}

	public List<XmlListItem> getListItems() {
		if(this.listItems!=null){
			Collections.sort(listItems,getListItemComparator());
		}else{
			listItems = new ArrayList<XmlListItem>();
		}
		
		return listItems;
	}

	public void setListItems(List<XmlListItem> listItems) {
		this.listItems = listItems;
	}

	public Float getLeading() {
		return leading;
	}

	public void setLeading(Float leading) {
		this.leading = leading;
	}

    public float getListItemIndent() {
        return listItemIndent;
    }

    public void setListItemIndent(float listItemIndent) {
        this.listItemIndent = listItemIndent;
    }

    @Override
    public com.itextpdf.text.List parse(PDFBuilder builder) throws Exception {
        XmlListSymbol symbol = this.getListSymbol();
        Chunk symbolChunk = new Chunk();
        
        if (symbol.getImage() != null) {
            Image img = Image.getInstance(builder.getResPath() + symbol.getImage().getSource());
            img.scaleAbsolute(symbol.getImage().getWidth(), symbol.getImage().getHeight());
            
            symbolChunk = new Chunk(img, -2, -2);
        } else if (symbol.getText() != null) {
            Font symbolFont = StyleUtil.createFont(symbol, builder.getStyleMap());
            symbolChunk = new Chunk(symbol.getText(), symbolFont);
        }
        com.itextpdf.text.List list = new com.itextpdf.text.List();
        list.setListSymbol(symbolChunk);
        list.setIndentationLeft(this.getIndent());
        //list.setLeading(this.getLeading());
        List<XmlListItem> listItems = this.getListItems();
        for (XmlListItem xmlListItem : listItems) {
            Paragraph p = xmlListItem.parse(builder);
            if(p != null){
                list.add(p);
            }
        }
        if(list.size() == 0){
            return list;
        }
        return list;
    }
}
